//
//  UIImage+Image.m
//  传智微博
//
//  Created by apple on 15-3-4.
//  Copyright (c) 2012年 itcast. All rights reserved.
//

#import "UIImage+Image.h"

@implementation UIImage (Image)

+ (instancetype)imageWithOriginalName:(NSString *)imageName
{
   UIImage *image = [UIImage imageNamed:imageName];
    
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


+ (instancetype)imageWithStretchableName:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
}

+ (instancetype)imageWithRoundName:(NSString *)imageName
{
    UIImage *img = [UIImage imageNamed:imageName];
    CGRect imageRect = CGRectMake(0, 0, img.size.width, img.size.height);
    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0);
    CGContextRef bitMapContext = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(bitMapContext, imageRect);
    CGContextClip(bitMapContext);
    [img drawInRect:imageRect];
    CGContextSetLineWidth(bitMapContext, 1);
    [[UIColor whiteColor] set];
    CGContextAddEllipseInRect(bitMapContext, imageRect);
    CGContextStrokePath(bitMapContext);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (instancetype)imageRoundWithImage:(UIImage *)image
{
    UIImage *img = image;
    CGRect imageRect = CGRectMake(0, 0, 100, 100);
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0.0);
    CGContextRef bitMapContext = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(bitMapContext, imageRect);
    CGContextClip(bitMapContext);
    [img drawInRect:imageRect];
    CGContextSetLineWidth(bitMapContext, 1);
    [[UIColor whiteColor] set];
    CGContextAddEllipseInRect(bitMapContext, imageRect);
    CGContextStrokePath(bitMapContext);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (instancetype)imageResizeWith:(UIImage *)image imageSize:(CGSize)size
{
    UIImage *img = image;
    CGRect imageRect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef bitMapContext = UIGraphicsGetCurrentContext();
    [img drawInRect:imageRect];
    CGContextStrokePath(bitMapContext);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (instancetype)imageWithHandleName:(NSString *)imageName
{
    UIImage *img = [UIImage imageNamed:imageName];
    CGRect imageRect = CGRectMake(0, 0, img.size.width, img.size.height);
    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0.0);
    CGContextRef bitMapContext = UIGraphicsGetCurrentContext();
    [img drawInRect:imageRect];
    CGContextStrokePath(bitMapContext);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+ (instancetype)imageAdaptationWithName:(NSString *)imageName
{
    if (kScreenWidth < 375) {

        return [UIImage imageNamed:imageName];
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
    
    return [[UIImage alloc] initWithContentsOfFile:path];
}


@end
